package com.aspire.boc.mms.wmf.servlets;

import javax.mail.Session;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import com.aspire.boc.mms.wmf.constants.GlobalConstant;
import com.aspire.boc.mms.wmf.permission.PermissionCacheService;
import com.aspire.boc.mms.wmf.util.EhcacheUtil;


/**
 * 配置信息装载
 * @author chenjianjun
 * @date 2013-5-9 下午3:05:53
 */
public class InitPermissionCache implements ServletContextListener {
	private  Log log = LogFactory.getLog(InitPermissionCache.class);
	@Autowired
	private PermissionCacheService permissionCacheService;
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		sce.getServletContext().setAttribute("firstComeIn", "true");
		log.info("contextInitialized param firstComeIn:true");
		try {
			ApplicationContext applicationContext = (ApplicationContext) sce.getServletContext().getAttribute(
					WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
			log.info("Get spring application context {}");
			sce.getServletContext().setAttribute("app", applicationContext);
//			permissionCacheService = ((PermissionCacheService) applicationContext.getBean("permissionCacheService"));
//			permissionCacheService.load();
			//log.info(permissionCacheService.getResourcesCache());
		} catch (Exception e) {
			log.error(">>>>>>>>>加载菜单和功能项失败,: e :" + e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
//		invalidate all active sessions;
		EhcacheUtil.removeAll(EhcacheUtil.CONFIG_CACHE);
		log.info(">>>>>>>>>InitPermissionCache 清除菜单配置到缓存完成("+  EhcacheUtil.getValue(EhcacheUtil.CONFIG_CACHE , 
				GlobalConstant.RESOURCE_CACHE)  +")");
	}
}
